from typing import List, Dict

from FileHandlers.JsonFile import JReadSave
from data.Singleton import Singleton


class NPCLoader(metaclass=Singleton):
    def __init__(self, saveName):
        self.save = saveName
        self.npcJson = {

        }
        self.class_update()  # 默认进行一次更新

    def class_update(self):
        """
        [pass] 将读取的数据在本地进行序列
        :return:
        """
        self.npcJson = JReadSave(
            saveName=self.save,
            filename="npc"
        )

    def getByName(self, name) -> dict:
        """
        [pass] 通过名称获取一个角色JSON数据
        :param name:
        :return:
        """
        self.class_update()
        return self.npcJson.get(name)

    def getByMethod(self, method) -> dict:
        """
        [pass] 查找 满足自定义条件的角色数据
        :param method:  会传入一个角色的名称和角色的属性两个参数 你需要返回一个布尔值类型来判断这个角色是否命中
        :return:
        """
        targets = []
        for chara_name, chara_attribute in self.npcJson.items():
            if method(chara_name, chara_attribute):
                targets.append({
                    chara_name: chara_attribute
                })
        return targets

    def getByAttributeValue(self, attribute, value) -> List[Dict]:
        """
        查找属性值和value一致的角色列表数据
        :param attribute:
        :param value:
        :return:
        """
        self.class_update()

        target = []
        for charaName, charaAttribute in self.npcJson.items():
            charaAttribute: dict  # 存储具体角色的所有属性的
            if charaAttribute.get(attribute) == value:
                target.append({charaName: charaAttribute})

        return target
